<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app"></div>
  <script src="../js/react.development.js"></script>
  <script src="../js/react-dom.development.js"></script>
  <script src="../js/prop-types.js"></script>
  <script src="../js/babel.min.js"></script>

  <script type="text/babel">
    class Life extends React.Component {

      state = {
        opacity: 1
      }
      death = () => {
        // 清除定时器
        //clearInterval(this.timer)
        ReactDOM.unmountComponentAtNode(document.getElementById("app"))
      }
      action = ()=>{
        this.timer = setInterval(() => {
          let { opacity } = this.state
          opacity -= 0.1
          if (opacity <= 0) opacity = 1
          this.setState({
            opacity
          })
        }, 200)
      }
      // 组件挂载完毕后调用
      componentDidMount() {
        console.log(123);
        this.action()
      }
      // 组件将要卸载的时候调用
      componentWillUnmount() {
        clearInterval(this.timer)
      }
      // 组件初始化后，状态改变后调用
      render() {
        return (
          <div>
            <h2 style={{ opacity: this.state.opacity }}>组件的生命周期,学不会怎么办？</h2>
            <button onClick={this.death}>不活了</button>
            {/*<button onClick={this.action}>开始变化</button>*/}
          </div>
        )
      }
    }

    ReactDOM.render(<Life />, document.getElementById("app"))
  </script>
</body>

</html>